package cn.kgc.tcst004.controller;


import cn.kgc.tcst004.pojo.Users;
import cn.kgc.tcst004.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;

    public UsersService getUsersService() {
        return usersService;
    }

    public void setUsersService(UsersService usersService) {
        this.usersService = usersService;
    }
//    登录
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    public ModelAndView login(Users users, HttpSession session){
        System.out.println(users.getUsername());
        ModelAndView modelAndView = new ModelAndView();
        Users selects = null;
        selects = usersService.selects(users);
        System.out.println("结果"+selects);
        if (selects != null){
            //登录成功跳转 list 页面
            session.setAttribute("id",selects.getId());
            modelAndView.setViewName("index");
            return modelAndView;
        }else{
//            登录失败跳回登录页面
            modelAndView.addObject("error", "用户名或密码不正确");
            modelAndView.setViewName("login");
            return modelAndView;
        }
    }
    @RequestMapping(value = "insert.do", method = RequestMethod.POST)
    public String insert(Users users, @RequestParam(required = false, value = "tp") MultipartFile picpath, HttpServletRequest request){
        System.out.println("进入添加用户");
        if (picpath != null) {
            String rea = request.getServletContext().getRealPath("image" + File.separator + "touxiang");
            String oldFileName = picpath.getOriginalFilename();
            String extension = FilenameUtils.getExtension(oldFileName);
            String fileName = picpath + ".jpg";
            users.setPicpath(fileName);
            List<String> list = new ArrayList();
            list.add("jpg");
            list.add("JPG");
            list.add("png");
            list.add("PNG");
            if (list.contains(extension)) {
                File targetFile = new File(rea, fileName);
                try {
                    picpath.transferTo(targetFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }}else{
                request.setAttribute("errormsg", "文件只能是图片");
            }
        }
        int i = usersService.insert(users);
        if(i > 0){
            System.out.println("添加用户成功");
            return "index";
        }else{
            System.out.println("添加用户失败");
            return "index";
        }
    }

    @RequestMapping(value = "getUsers.do",method = RequestMethod.POST)
    public void getUsers(HttpServletRequest request, HttpServletResponse response){
        int pageIndex = 1;
        int pageSize = 1;
        if (request.getParameter("pageIndex")!=null&&request.getParameter("pageIndex").length()>0) {
            pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<Users> users = usersService.getUsers();
        PageInfo<Users> pageInfo = new PageInfo<Users>(users);
        try {
            response.getWriter().print(new Gson().toJson(pageInfo));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
